package com.brp.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.brp.controller.CargoController;
import com.brp.po.Cargo;
import com.brp.dao.CargoMapper;
import com.brp.service.ICargoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.brp.vo.CarBuyVo;
import com.brp.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author all
 * @since 2022-03-16
 */
@Service
public class CargoServiceImpl extends ServiceImpl<CargoMapper, Cargo> implements ICargoService {
    @Autowired
    private CargoMapper cargoMapper;

    public IPage<Cargo> selectPage(Page<Cargo> page){
        IPage<Cargo> cargos = cargoMapper.selectPage(page,null);
        return cargos;
    }

    @Override
    public boolean updateCargo(Wrapper<Cargo> wrapper) {
        return cargoMapper.updateCargo(wrapper) > 0 ? true : false;
    }

    @Override
    public ResultVo<CarBuyVo> getAllLike(String cargoVender) {
        List<Cargo> allLike = cargoMapper.getAllLike(cargoVender);
        int count=cargoMapper.getCount(cargoVender);

        List<CarBuyVo>carBuyVoList=new ArrayList<>();
        for (int i = 0; i <allLike.size() ; i++) {
            Cargo cargo=allLike.get(i);
            CarBuyVo carBuyVo=new CarBuyVo();

            carBuyVo.setCargoId(cargo.getCargoId());
            carBuyVo.setCargoVender(cargo.getCargoVender());
            carBuyVo.setCargoType(cargo.getCargoType());

            carBuyVo.setBuyrecordId(cargo.getBuyrecord().getBuyrecordId());
            carBuyVo.setBuyrecordNumber(cargo.getBuyrecord().getBuyrecordNumber());

            carBuyVoList.add(carBuyVo);
        }

        ResultVo<CarBuyVo>carBuyVoResultVo=new ResultVo<>(count,carBuyVoList);

        return carBuyVoResultVo;
    }


}
